package com.fyx.timedTask;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyx.config.FiveMongoConfig;
import com.fyx.config.SecondaryMongoConfig;
import com.fyx.config.SixthMongoConfig;
import com.fyx.entity.BigOrSmallEntity;
import com.fyx.entity.StaticeEntity;
import com.fyx.mapper.*;
import com.fyx.until.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Component(value="BigOrSmallTask")
public class BigOrSmallTask {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Resource(name = SecondaryMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate secondaryMongoTemplate;

    @Resource(name = SixthMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate sixthMongoTemplate;

    @Autowired
    private SysDictDetailMapper sysDictDetailMapper;

    @Autowired
    private BigOrSmallMapper bigOrSmallMapper;

    public void run(){
        log.info("猜大小人数记录任务：执行开始=======================");
        Long beginTime= DateUtil.getDayBegin().getTime()/1000;
        long nowTime=new Date().getTime()/1000;
        String tableName=DateUtil.timeStamp2Date(beginTime+"","yyyyMM");;
        if(beginTime==nowTime){
            beginTime=DateUtil.getBeginDayOfYesterday().getTime()/1000;
            if(!DateUtil.timeStamp2Date(beginTime+"","yyyyMM").equals(DateUtil.timeStamp2Date(nowTime+"","yyyyMM"))){
                tableName =DateUtil.timeStamp2Date(beginTime+"","yyyyMM");
            }
        }
        long endTime=beginTime+86400;
        record(beginTime,endTime,tableName);
        log.info("猜大小人数记录任务：执行完毕=======================");
    }

    private void record(Long beginTime,Long endTime,String tableName) {

        BigOrSmallEntity bigOrSmallEntity=new BigOrSmallEntity();
        bigOrSmallEntity.setGolds(goldCount(beginTime, endTime,tableName,"hzmj"));
        bigOrSmallEntity.setPeopleNumbers(peopleNumber( beginTime, endTime, tableName, null,"hzmj"));
        bigOrSmallEntity.setGameCounts(gameCount(beginTime,endTime,tableName, null,"hzmj"));
        String result="{";
        Object object[]={101,201,301,401,501,601,701,801,901,1001,1101};
        result+="\"1\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"hzmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"hzmj") +"},";
        object=new Object[]{102,202,302,402,502,602,702,802,902,1002,1102};
        result+="\"2\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"hzmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"hzmj") +"},";
        object=new Object[]{103,203,303,403,503,603,703,803,903,1003,1103};
        result+="\"3\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"hzmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"hzmj") +"},";
        object=new Object[]{104,204,304,404,504,604,704,804,904,1004,1104};
        result+="\"4\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"hzmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"hzmj") +"},";
        object=new Object[]{105,205,305,405,505,605,705,805,905,1005,1105};
        result+="\"5\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"hzmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"hzmj") +"}}";
        bigOrSmallEntity.setTheValue(result);
        bigOrSmallEntity.setFlag("hzmj");
        bigOrSmallEntity.setTime(beginTime);
        QueryWrapper<BigOrSmallEntity> wrapper=new QueryWrapper();
        wrapper.eq("time",beginTime);
        wrapper.eq("flag","hzmj");
        BigOrSmallEntity bigOrSmallEntity1=bigOrSmallMapper.selectOne(wrapper);
        if(bigOrSmallEntity1==null) {
            bigOrSmallMapper.insert(bigOrSmallEntity);
        }else {
            bigOrSmallMapper.update(bigOrSmallEntity,wrapper);
        }
        bigOrSmallEntity=new BigOrSmallEntity();
        bigOrSmallEntity.setGolds(goldCount(beginTime, endTime,tableName,"clmj"));
        bigOrSmallEntity.setPeopleNumbers(peopleNumber( beginTime, endTime, tableName, null,"clmj"));
        bigOrSmallEntity.setGameCounts(gameCount(beginTime,endTime,tableName, null,"clmj"));
        result="{";
        object=new Object[]{101,201,301,401,501,601,701,801,901,1001,1101};
        result+="\"1\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"clmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"clmj") +"},";
        object=new Object[]{102,202,302,402,502,602,702,802,902,1002,1102};
        result+="\"2\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"clmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"clmj") +"},";
        object=new Object[]{103,203,303,403,503,603,703,803,903,1003,1103};
        result+="\"3\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"clmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"clmj") +"},";
        object=new Object[]{104,204,304,404,504,604,704,804,904,1004,1104};
        result+="\"4\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"clmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"clmj") +"},";
        object=new Object[]{105,205,305,405,505,605,705,805,905,1005,1105};
        result+="\"5\":{\"peopleNumber\":"+peopleNumber( beginTime, endTime, tableName, object,"clmj");
        result+=",\"gameCount\":"+gameCount(beginTime,endTime,tableName, object,"clmj") +"}}";
        bigOrSmallEntity.setTheValue(result);
        bigOrSmallEntity.setFlag("clmj");
        bigOrSmallEntity.setTime(beginTime);
        wrapper=new QueryWrapper();
        wrapper.eq("time",beginTime);
        wrapper.eq("flag","clmj");
        bigOrSmallEntity1=bigOrSmallMapper.selectOne(wrapper);
        if(bigOrSmallEntity1==null) {
            bigOrSmallMapper.insert(bigOrSmallEntity);
        }else {
            bigOrSmallMapper.update(bigOrSmallEntity,wrapper);
        }
    }

    private Long goldCount(Long beginTime,Long endTime,String tableName,String flag){
        tableName="gold_rec_"+tableName;
        Criteria criteria=new Criteria();
        criteria.and("time").gte(beginTime).lt(endTime);
        criteria.and("way").is(86);
        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                Aggregation.group().sum("num").as("v4"));
        AggregationResults<StaticeEntity> ar;
        if(flag.equals("hzmj")){
           ar =secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        }else {
            ar =sixthMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        }
        List<StaticeEntity> list = ar.getMappedResults();
        if(list.size()>0){
            return list.get(0).getV4();
        }else {
            return 0L;
        }
    }

    private Long peopleNumber(Long beginTime,Long endTime,String tableName,Object object[],String flag){
        tableName="big_or_small_"+tableName;
        Criteria criteria=new Criteria();
        criteria.and("time").gte(beginTime).lt(endTime);
        if(object!=null){
            criteria.and("placeid").in(object);
        }

        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                Aggregation.group("pid").count().as("v4"),
                Aggregation.group("pid").count().as("v4")
        );
        AggregationResults<StaticeEntity> ar ;
        if(flag.equals("hzmj")){
            ar =secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        }else {
            ar =sixthMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        }
        List<StaticeEntity> list = ar.getMappedResults();

        if(list.size()>0){
            return list.get(0).getV4();
        }else {
            return 0L;
        }
    }
    private Long gameCount(Long beginTime,Long endTime,String tableName,Object object[],String flag){
        tableName="big_or_small_"+tableName;
        Criteria criteria=new Criteria();
        criteria.and("time").gte(beginTime).lt(endTime);
        criteria.and("turn_count").is(0);
        if(object!=null){
            criteria.and("placeid").in(object);
        }
        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                Aggregation.group().count().as("v4"));
        AggregationResults<StaticeEntity> ar;
        if(flag.equals("hzmj")){
            ar =secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        }else {
            ar =sixthMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        }
        List<StaticeEntity> list = ar.getMappedResults();
        if(list.size()>0){
            return list.get(0).getV4();
        }else {
            return 0L;
        }
    }
}
